package com.mths.client.page;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Button;
import com.mths.client.component.ComboBox;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.CompanyDocumentController;
import com.mths.client.controller.CompanyDocumentControllerAsync;
import com.mths.client.controller.DocumentController;
import com.mths.client.controller.DocumentControllerAsync;
import com.mths.client.dto.CompanyDocumentDTO;
import com.mths.client.dto.MetaDataDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ListItem;
import com.mths.client.util.PageUtil;
import com.mths.client.widget.DocumentMetaWidget;
import com.mths.client.widget.MessageBox;

public class CompanyDocumentInsert extends AbstractPage implements IPage {

	private static CompanyDocumentInsertUiBinder	uiBinder	= GWT.create(CompanyDocumentInsertUiBinder.class);

	interface CompanyDocumentInsertUiBinder extends UiBinder<Widget, CompanyDocumentInsert> {
	}

	private final CompanyDocumentControllerAsync	compDocController	= GWT.create(CompanyDocumentController.class);
	private final DocumentControllerAsync			documentAsync		= GWT.create(DocumentController.class);

	@UiField
	ComboBox										docClassCmb, docTypeCmb;

	@UiField
	DocumentMetaWidget								docMetaWidget;

	@UiField
	Button											docSaveBtn, closeBtn;

	private boolean									fileUploaded		= false;

	private CompanyDocumentDTO						dto					= new CompanyDocumentDTO();

	public CompanyDocumentInsert() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		loadDocClassData();
	}

	private void initEventHandler() {
		docClassCmb.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (docClassCmb.getFieldValue() != null && !docClassCmb.getFieldValue().isEmpty()) {
					docTypeCmb.clear();
					docMetaWidget.clear();
					loadDocTypeCombo();
				}
			}
		});
		docTypeCmb.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (docTypeCmb.getFieldValue() != null && !docTypeCmb.getFieldValue().isEmpty()) {
					loadDocMetaWidget();
				}
			}
		});

		docSaveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveDocument();
			}

		});

		closeBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.COMPANY_DOCUMENT);
			}
		});
	}

	private void saveDocument() {
		if (!fileUploaded) {
			new MessageBox("Lütfen Dosya Yükleyiniz");
			return;
		}
		dto.setDocClass(docClassCmb.getFieldValue());
		dto.setDocType(docTypeCmb.getFieldValue());
		dto.setFieldValueMap(docMetaWidget.getFieldsValueMap());
		dto.setFileSessionKey(docMetaWidget.getUploadFileCmp().getFileSessionKey());

		compDocController.saveDocument(dto, new AsyncCall<Void>() {
			@Override
			public void successCall(Void result) {
				new MessageBox("İşleminiz başarılı bir şekilde sonuçlanmıştır");
				docSaveBtn.setEnabled(false);
				docMetaWidget.clear();
			}
		});
	}

	private void loadDocClassData() {
		documentAsync.loadDocClassCombo("", new AsyncCall<List<ListItem>>() {
			@Override
			public void successCall(List<ListItem> result) {
				docClassCmb.addItemList(result);
			}

		});
	}

	private void loadDocTypeCombo() {
		documentAsync.loadDocTypeCombo(docClassCmb.getFieldValue(), new AsyncCall<List<ListItem>>() {

			@Override
			public void successCall(List<ListItem> result) {
				docTypeCmb.addItemList(result);
			}
		});
	}

	private void loadDocMetaWidget() {
		documentAsync.loadMeataWidgetByDocType(docTypeCmb.getFieldValue(), new AsyncCall<List<MetaDataDTO>>() {
			@Override
			public void successCall(List<MetaDataDTO> result) {
				docMetaWidget.clear();
				docMetaWidget.loadWidgetComponent(result);
				initUploadHandler();
			}
		});
	}

	private void initUploadHandler() {
		docMetaWidget.getUploadFileCmp().addCompletedCallback(new AsyncCallback<SubmitCompleteEvent>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(SubmitCompleteEvent result) {
				fileUploaded = true;
				docSaveBtn.setEnabled(true);
			}
		});

		docMetaWidget.getUploadFileCmp().addFileChangedCallback(new AsyncCallback<ChangeEvent>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(ChangeEvent result) {
				fileUploaded = false;
				docSaveBtn.setEnabled(false);
			}
		});
	}

	@Override
	public String pageName() {
		return messages.CompanyDocumentInsert_pageName();
	}
}